//
//  WKNewViewController.m
//  WKDemo
//
//  Created by wangzhaohui-Mac on 14-8-2.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKTwoNewViewController.h"
#import "WKOnlyTextHttpTool.h"
#import "WKOnlyTextParam.h"
#import "WKOnlyTextResult.h"
#import "WKTableViewCell.h"
#import "WKTableViewCellFrame.h"
#import "WKHomeHttpTool.h"
#import "WKTopTableViewCellFrame.h"
#import "WKCellPuthinfoTableView.h"

@interface WKTwoNewViewController ()
/**模型数组*/
@property (nonatomic,strong) NSMutableArray *statuses;
@property (nonatomic , strong)WKTableViewCellFrame *tablecell;

@property (nonatomic, strong)WKCellPuthinfoTableView *puthCellView;

@end

@implementation WKTwoNewViewController
- (UINavigationController *)navigation
{
    if(_navigation == nil)
    {
        UINavigationController *Navigation = [[UINavigationController alloc]init];
        _navigation = Navigation;
    }
    return _navigation;
}

- (NSMutableArray *)statuses
{
    if(_statuses == nil)
    {
        _statuses = [NSMutableArray array];
    }
    return _statuses;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
//    [self loadNewdata];
    
//    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    
//    self.tableView.backgroundColor = WKColor(211, 211, 211);
//    //上拉刷新
//    [self.tableView addFooterWithTarget:self action:@selector(loacldMore)];
//    //下拉刷新
//    [self.tableView addHeaderWithTarget:self action:@selector(loacldNew)];
}
/**
 *  加载新数据
 */
- (void)loadNewdata
{
    WKOnlyTextParam *param = [[WKOnlyTextParam alloc] init];
    [WKOnlyTextHttpTool getNewestNewDataListWithParam:param success:^(WKOnlyTextResult *result) {
    //如果返回数据为空则返回;
    if (result == nil) return;
        self.statuses = nil;
    //        //[self.statuses addObjectsFromArray:result.homeDatas];
    NSRange range = NSMakeRange(0, result.homeDatas.count);
    NSIndexSet *set = [NSIndexSet indexSetWithIndexesInRange:range];
    [self.statuses insertObjects:result.homeDatas atIndexes:set];
        
    //显示底部刷新控件
    self.tableView.footerHidden = NO;
    //刷新数据
    [self.tableView reloadData];
    
    //停止下拉刷新
    [self.tableView headerEndRefreshing];
    } failure:^(NSError *error) {
        WKLog(@"%@",error);
    }];

}
/**
 *  加载更多数据
 */
- (void)loadMoreData
{
    WKOnlyTextParam *param = [[WKOnlyTextParam alloc] init];
    [WKOnlyTextHttpTool getNewestMoreDataListWithParam:param success:^(WKOnlyTextResult *result) {
        //如果返回数据为空则返回;
        if (result == nil) return;
        [self.statuses addObjectsFromArray:result.homeDatas];

        //刷新数据
        [self.tableView reloadData];
        //停止下拉刷新
        [self.tableView footerEndRefreshing];
    } failure:^(NSError *error) {
        WKLog(@"%@",error);
    }];
}

- (WKTableViewCellFrame *)tablecell
{
    if(_tablecell == nil)
    {
        WKTableViewCellFrame *tablecell = [[WKTableViewCellFrame alloc]init];
        _tablecell = tablecell;
    }
    return _tablecell;
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statuses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WKTableViewCell *cell = [WKTableViewCell cellWithTableView:tableView];
    self.tablecell.status = self.statuses[indexPath.row];
    cell.tableCellFrame = self.tablecell;

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //去掉cell的点击
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //跳转控制器
    WKCellPuthinfoTableView *cellinfo = [[WKCellPuthinfoTableView alloc]init];
    
    cellinfo.cellFreme.status = self.statuses[indexPath.row];
    cellinfo.cellFreme = self.tablecell;
    
    [self.navigation pushViewController:cellinfo animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WKTableViewCellFrame *cell = [[WKTableViewCellFrame alloc]init];
    cell.status = self.statuses[indexPath.row];
    return cell.cellHeight;
}
@end
